using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace MTRC.ODMS.Web.SOM.DutySCRegister
{
    /// <summary>
    /// Author:SQB
    /// </summary>
    public partial class DutySCRegisterEnquiry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140701";
            }
        }

        #endregion

        //edit by hjh 20120516 for issue 1531
        //protected int TempDataID
        //{
        //    get { return int.Parse(ViewState["TempDataID"].ToString()); }
        //    set {ViewState["TempDataID"] = value;}
        //}
        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grvData.PageSize = this.GridViewPageDefaultSize();
                FunctionItemSetting();
                SetLoadPage();
                SetLanguage();
            }
            
        }

        private void SetLoadPage()
        {
            this.btnExport.Enabled = false;
            int days = int.Parse(DAL.CommonData.GetNumVal("SOM_DutySCRegisterEnquiryDateRange").ToString());
            this.dtbFrom.SetDateTime(MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime().AddDays(-days));
            this.dtbTo.SetDateTime(MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime());
        }
        #endregion

        #region SetLanguage

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
            FunctionItemSetting();
            
        }

        private void SetLanguage()
        {
            this.lblFrom.Text = this.GetLabel("COM_From", true);
            this.lblTo.Text = this.GetLabel("COM_To", true);
            this.lblStaion.Text = this.GetLabel("SOM_Station", true);
            this.lblDisplay.Text = this.GetLabel("COM_Show", true);
            this.chkFlag.Text = this.GetLabel("SOM_Voided");
            this.btnSearch.Text = this.GetLabel("COM_Search");
            this.btnExport.Text = this.GetLabel("COM_Export");
            this.btnNew.Text = this.GetLabel("SOM_AddNewAssetLoanItem");
            BindData();
            
        }

        #endregion

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("DutySCRegisterEntry.aspx");
        }


        /// <summary>
        /// setting user access by function item.
        /// </summary>
        /// 

        private void FunctionItemSetting()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140701_F1", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247 Can not Test
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else if (DAL.Security.User.CanFunctionItemAccess("M140701_F2", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //BindStation
                //update by gordon 20151020 U247 Can not Test
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else
            {
                //update by gordon 20151020 U247 Can not Test
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
                this.ddlStation.Enabled = false;
            }

            //this.ddlStation.Items.Insert(0, this.LiALL());

            if (DAL.Security.User.CanFunctionItemAccess("M140701_F3", this.ODMUser.UserProfileID, this.MenuID)) //readonly
            {
                this.btnNew.Enabled = false;
            }

            //update by gordon 20151020 U247 Can not Test
            //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null)
            //{
            //    this.ddlStation.SelectedValue = this.ODMUser.StationID.ToString();
            //}
            if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null)
            {
                this.ddlStation.SelectedValue = this.ODMUser.LocationID.ToString();
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (CheckFromToDate() == false)
            {
                this.Alert(this.GetMessage("COM_StartEndDate"));
                return;
            }
            BindData();
        }
        /// <summary>
        /// Check From Date and To Date
        /// </summary>
        /// 

        private bool CheckFromToDate()
        {
            if (this.dtbFrom.GetDateTime() != DateTime.MinValue && this.dtbTo.GetDateTime() != DateTime.MinValue)
            {
                if (InvalidateInputDate(this.dtbFrom.GetDateTime(), this.dtbTo.GetDateTime()))
                {
                    return false;
                }
            }
            return true;
        }
        
        private void BindData()
        {
            //edit by hjh 20120516 for issue 1531
            DataTable dt = getData();
            //TempDataID = MTRC.ODMS.BLL.SOM.TempData.CacheDataSet(dt.DataSet, this.ODMUser.ID, this.ODMUser.Domain);
            this.grvData.DataSource = dt.DefaultView;
            this.grvData.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
            this.grvData.DataBind();
            
            //Set Export button enabled
            if (dt.Rows.Count <= 0)
            {
                this.btnExport.Enabled = false;
            }
            else
            {
                this.btnExport.Enabled = true;
            }

            dt.Dispose();
            dt = null;
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                ////huangyao 20081129 in MTR office
                string urlString = "DutySCRegisterEntry.aspx?Action=New&StationEventID="+this.grvData.DataKeys[e.Row.RowIndex]["StationEventID"].ToString();
                string strScript = "OpenEntryPageMaximum('" + urlString + "');";
                e.Row.Cells[0].Text = "<a style='cursor: pointer;' target='_blank' onclick=" + strScript + ">" + e.Row.Cells[0].Text + "</a>";
                //style='cursor: pointer; font-weight: bold;'  //and this is can done also
                e.Row.Cells[0].Font.Bold = true;
                //----------------------------------------
                if (((Label)e.Row.Cells[6].FindControl("lblStatusID")).Text == "1")
                {
                    ((Image)e.Row.Cells[6].FindControl("imgStatus")).ImageUrl = "../../Images/SOM/EventStatus_OS.gif";
                    ((Image)e.Row.Cells[6].FindControl("imgStatus")).ToolTip = this.GetLabel("SOM_OS");
                }
                else if (((Label)e.Row.Cells[6].FindControl("lblStatusID")).Text == "2")
                {
                    ((Image)e.Row.Cells[6].FindControl("imgStatus")).ImageUrl = "../../Images/SOM/EventStatus_COM.gif";
                    ((Image)e.Row.Cells[6].FindControl("imgStatus")).ToolTip = this.GetLabel("SOM_Completed");
                }
                else if (((Label)e.Row.Cells[6].FindControl("lblStatusID")).Text == "3")
                {
                    ((Image)e.Row.Cells[6].FindControl("imgStatus")).ImageUrl = "../../Images/SOM/EventStatus_Void.gif";
                    ((Image)e.Row.Cells[6].FindControl("imgStatus")).ToolTip = this.GetLabel("SOM_Voided");
                }

                //update by LiangZehong on 2008-12-16, for datetime show as two lines Begin...
                //e.Row.Cells[1].Text = this.FormatDateTime(DateTime.Parse(e.Row.Cells[1].Text), true);
                e.Row.Cells[1].Text = this.FormatDateTime_AddBR(e.Row.Cells[1].Text);
                //End...
            }
             
        }

        protected void grvData_NeedDataBind()
        {
            BindData();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //edit by hjh 20120516 for issue 1531
            if (CheckFromToDate() == false)
            {
                this.Alert(this.GetMessage("COM_StartEndDate"));
                return;
            }

            string fileName = "";
            fileName = MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime().ToString() + System.Guid.NewGuid().ToString();

            //edit by hjh 20120516 for issue 1531
            DataSet dsReturn = this.getData().DataSet;
            //if (MTRC.ODMS.BLL.SOM.TempData.GetCacheData(TempDataID) != null)
            //{
            //    dsReturn = MTRC.ODMS.BLL.SOM.TempData.GetCacheData(TempDataID);
            //}
            object limitExpCount = DAL.CommonData.GetNumVal("SOM_DutySCExportMAXControl");
            if (dsReturn == null)
            {
                return;
            }
            else if (limitExpCount != null && dsReturn.Tables[0].Rows.Count > int.Parse(limitExpCount.ToString()))//edit by hjh 20120516 for issue 1531
            {
                Alert(string.Format(this.GetMessage("SOM_ExportLimitControl"), int.Parse(limitExpCount.ToString())));
                return;
            }

            DataTable dtSearchResult = dsReturn.Tables[0];
            dtSearchResult.Columns.Remove("StationEventID");
            dtSearchResult.Columns.Remove("EventStatusID");
            dtSearchResult.Columns[0].ColumnName = this.GetLabel("SOM_RegNo");
            dtSearchResult.Columns[1].ColumnName = this.GetLabel("SOM_RegDateTime");
            dtSearchResult.Columns[2].ColumnName = this.GetLabel("SOM_Shift");
            dtSearchResult.Columns[3].ColumnName = this.GetLabel("SOM_StaffID");
            dtSearchResult.Columns[4].ColumnName = this.GetLabel("SOM_StaffName");
            dtSearchResult.Columns[5].ColumnName = this.GetLabel("Com_Title");
            dtSearchResult.Columns[6].ColumnName = this.GetLabel("COM_Status");
            //Common.ExportExcel_showHHMM(dsReturn, Context, "DutySCRegisterEnquiry" + fileName + ".xls");
            Common.ExportExcel_showHHMM(dsReturn, Context, "DutySCRegisterEnquiry.xls");
            dtSearchResult.Dispose();
            dsReturn.Dispose();
            dtSearchResult = null;
            dsReturn = null;
        }
        //edit by hjh 20120516 for issue 1531
        private DataTable getData()
        {
            DataTable dt = new DataTable();
            string stationIDs = "";
            SetStationSelectedItem(ref stationIDs, this.ddlStation);
            dt = MTRC.ODMS.DAL.SOM.Event.StationEventSCBookOnDa.GetDutySCReg(this.LangCode, this.dtbFrom.GetDateTime(), this.dtbTo.GetDateTime(), stationIDs, this.chkFlag.Checked);

            return dt;
        }
    }
}
